package org.sn.jdish.spark.streaming;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaPairInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.kafka.KafkaUtils;

import com.google.common.collect.Lists;

import kafka.serializer.StringDecoder;
import scala.Tuple2;

/**
 * KafKa作为数据源--生产者,SparkStreaming作为消费者。此方法 使用Direct创建
 * 
 * @author snzigod@hotmail.com
 */
public final class JavaKafkaDirectWordCount {
	private static final Pattern SPACE = Pattern.compile(" ");

	/**
	 * 1.一对一 2.高效 3.准确的只计算一次
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		SparkConf sparkConf = new SparkConf().setAppName("sparkStreaming").setMaster("local[5]");
		/*
		 * 创建该对象类似于spark core中的JavaSparkContext
		 * 该对象除了接受SparkConf对象，还接收了一个BatchInterval参数,就算说， 没收集多长时间去划分一个人Batch即RDD去执行
		 */
		JavaStreamingContext sc = new JavaStreamingContext(sparkConf, Durations.seconds(5));

		readJson(sc);

		sc.start();
		sc.awaitTermination();

		sc.stop();
		sc.close();
	}

	static void readJson(JavaStreamingContext sc) {
		Map<String, String> kafkaParams = new HashMap<String, String>(); // key是topic名称,value是线程数量
		kafkaParams.put("metadata.broker.list", "master:9092,slave1:9092,slave2:9092"); // 指定broker在哪
		HashSet<String> topicsSet = new HashSet<String>();
		topicsSet.add("2017-7-26"); // 指定操作的topic

		// Create direct kafka stream with brokers and topics createDirectStream()
		JavaPairInputDStream<String, String> messages = KafkaUtils.createDirectStream(sc, String.class, String.class,
				StringDecoder.class, StringDecoder.class, kafkaParams, topicsSet);

		JavaDStream<String> lines = messages.map(new Function<Tuple2<String, String>, String>() {
			private static final long serialVersionUID = 1L;

			@Override
			public String call(Tuple2<String, String> tuple2) {
				return tuple2._2();
			}
		});

		JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Iterable<String> call(String x) {
				return Lists.newArrayList(SPACE.split(x));
			}
		});

		JavaPairDStream<String, Integer> wordCounts = words.mapToPair(new PairFunction<String, String, Integer>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Tuple2<String, Integer> call(String s) {
				return new Tuple2<String, Integer>(s, 1);
			}
		}).reduceByKey(new Function2<Integer, Integer, Integer>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Integer call(Integer i1, Integer i2) {
				return i1 + i2;
			}
		});

		wordCounts.print();
	}
}
